<template>
  <div class="mod-demo-echarts">
    <el-row :gutter="20">
      <el-col  class="card-panel-col">
        <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
           <el-form-item label="指导教师">
             <el-select v-model="dataForm.account" @change="changeStatistics()" placeholder="请选择"style="width: 240px;"clearable>
                 <el-option
                   v-for="item in teacherName"
                   :key="item.account"
                   :label="item.userName"
                   :value="item.account">
                 </el-option>
               </el-select>
           </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="24">
        <el-card>
          <div id="J_chartBarBox" class="chart-box"></div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  /* eslint-disable */
  import echarts from 'echarts'
  export default {
    data () {
      return {
        chartBar: null,
        dataForm: {
          account: ''
        },
        teacherName: [],
        courseInfo: [],
        SeriesInfo: []
      }
    },
    mounted () {
      this.initChartBar()
    },
    activated () {
      this.getExperimentList()
      if (this.chartBar) {
        this.chartBar.resize()
      }
    },
    methods: {
      //横坐标数据统计
      getDataX (){
        this.$http({
          url: this.$http.adornUrl('/adminCourse/getExperimentNameByAccount'),
          method: 'get',
          params: this.$http.adornParams({
           'account': this.dataForm.account
          })
        }).then(({data}) => {
          if(data.success){
            this.courseInfo = data.data
             this.initChartBar()
          } else {
            this.$message.error(data.message)
          }
        })
      },
      //条形图数据
      getSeriesData (){
        this.$http({
          url: this.$http.adornUrl('/statistics/getWorkStatistics'),
          method: 'get',
          params: this.$http.adornParams({
           'account': this.dataForm.account
          })
        }).then(({data}) => {
          if(data.success){
            console.log("条形图数据data.data",data.data)
            this.SeriesInfo = data.data
             this.initChartBar()
          } else {
            this.$message.error(data.message)
          }
        })
      },
      //统计数据
      changeStatistics(){
        this.getDataX()
        this.getSeriesData()
      },
      // 获取教师信息
      getExperimentList () {
        this.$http({
          url: this.$http.adornUrl('/teacher/getTeacherName'),
            method: 'get'
        }).then(({data}) => {
          if(data.success){
            this.teacherName = data.data
            this.dataForm.account = data.data[0].account
            // this.getStatistics()
            this.changeStatistics()
          } else {
            this.$message.error(data.message)
          }
        })
      },
     // 柱状图
      initChartBar () {
        var option = {
          tooltip: {
            trigger: 'axis',
            formatter : function(params) {
                    var result = params[0].name;
                    params.forEach(function(item) {
                            if (item.data) {
                                result += '<br/>' + item.seriesName + ' : ' + item.data + '%';
                            }
                        });
                    return result;
                },
            axisPointer: {
              type: 'shadow'
            }
          },
          legend: {
            data: ['实验完成率', '作业完成率']
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: [
            {
              show:true,
              type: 'category',
              data: this.courseInfo,
			  interval:0
            }
          ],
          yAxis: [
            {
               type: 'value',
               axisLabel: {
                     show: true,
                     interval: 'auto',
                     formatter: '{value} %'
                   },
               show: true
            }
          ],
          series: this.SeriesInfo
          // series: [
          //   {
          //     name: '实验完成率',
          //     type: 'bar',
          //     barWidth: 15,
          //     data: [10, 20,30]
          //   },
          //   {
          //     name: '作业完成率',
          //     type: 'bar',
          //     barWidth: 15,
          //     data: [50, 60]
          //   }
          // ]
        }
        this.chartBar = echarts.init(document.getElementById('J_chartBarBox'))
        this.chartBar.setOption(option)
        window.addEventListener('resize', () => {
          this.chartBar.resize()
        })
      },
     }
  }
</script>

<style lang="scss">
  .mod-demo-echarts {
    > .el-alert {
      margin-bottom: 10px;
    }
    > .el-row {
      margin-top: -10px;
      margin-bottom: -10px;
      .el-col {
        padding-top: 10px;
        padding-bottom: 10px;
      }
    }
    .chart-box {
      min-height: 400px;
    }
  }
</style>
